package com.xyz.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.xyz.controller.vo.InStockVO;
import com.xyz.request.PartInnOrderRequest;
import com.xyz.request.PartInnStockRequest;
import com.xyz.service.PartInnStockService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("part/inn/stock")
public class PartInnStockController {

    private static final Logger logger = LoggerFactory.getLogger(PartInnStockController.class);


    @Autowired
    private PartInnStockService partInnStockService;

    /**
     * 内库库存入库
     */
    @PostMapping("instock")
    public DataResult createInStock(
            LoginInfo li,
            @RequestBody List<InStockVO> inStockVOList) {
        try {
            logger.info(li.getLoginEmployeeName());
            logger.info(JSON.toJSONString(inStockVOList));
            return partInnStockService.createInStock(li, inStockVOList);
        } catch (Exception e) {
            logger.error("createInStock", e);
            return DataResult.build9500();
        }
    }


    /**
     * 查询内库列表
     */
    @PostMapping("list")
    public DataResult selectList(LoginInfo li, @RequestBody PartInnStockRequest param) {
        try {
            logger.info("查询内库列表入参：{}", JSONUtil.toJsonStr(param));
            return partInnStockService.selectList(li, param);
        } catch (Exception e) {
            logger.error("selectList", e);
            return DataResult.build9500();
        }
    }

    /**
     * 查询内库出库记录
     */
    @PostMapping("out/record/list")
    public DataResult selectStockOutRecordList(LoginInfo li, @RequestBody PartInnOrderRequest param) {
        try {
            logger.info("查询内库出库记录入参：{}", JSONUtil.toJsonStr(param));
            return partInnStockService.selectStockOutRecordList(li, param);
        } catch (Exception e) {
            logger.error("selectStockOutRecordList", e);
            return DataResult.build9500();
        }
    }


    /**
     * 查询内库出库详情
     */
    @PostMapping ("out/record/info")
    public DataResult selectStockOutRecordInfo(LoginInfo li, Long id) {
        try {
            logger.info("查询内库出库详情入参：{}", JSONUtil.toJsonStr(id));
            return partInnStockService.selectStockOutRecordInfo(li, id);
        } catch (Exception e) {
            logger.error("selectStockOutRecordInfo", e);
            return DataResult.build9500();
        }
    }


    /**
     * 内库库存导出
     */
    @GetMapping("export")
    public void export(LoginInfo li, PartInnStockRequest param, HttpServletResponse response) {
        try {
            logger.info("内库出入库记录导出入参：{}", JSONUtil.toJsonStr(param));
             partInnStockService.export(param, response);
        } catch (Exception e) {
            logger.error("导出内库出入库记录失败", e);
        }
    }

    /**
     * 内库库存导入
     * @param li
     * @param file
     * @return
     */
    @PostMapping("import")
    public DataResult importExcel(LoginInfo li, @RequestParam("file") MultipartFile file) {
        try {
            logger.info("内库出入库记录导入");
            return partInnStockService.importExcel(li, file);
        } catch (Exception e) {
            logger.error("内库excel导入失败", e);
            return DataResult.build9500();
        }
    }


    /**
     * 内库库存记录导出
     */
    @GetMapping("/inn/stock/export")
    public void innStockExport(LoginInfo li, PartInnOrderRequest param, HttpServletResponse response) {
        try {
            logger.info("内库出入库记录导出入参：{}", JSONUtil.toJsonStr(param));
            partInnStockService.innStockExport(param, response);
        } catch (Exception e) {
            logger.error("导出内库出入库记录失败", e);
        }
    }

}
